package com.gy.paidao;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.gy.paidao.bean.BaseBean;
import com.gy.paidao.person.LoginActivity;
import com.gy.paidao.person.RegisterActivity;
import com.gy.paidao.person.RegisterEmailActivity;
import com.gy.paidao.person.bean.LoginBean;
import com.gy.paidao.setting.SettingMainActivity;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.LogUtil;
import com.gy.paidao.util.SPHelper;
import com.gy.paidao.util.Urls;
import com.jky.struct2.http.core.AjaxParams;

import org.apache.http.client.CookieStore;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.List;

/**
 * 作者：gy on 16/2/20
 * 说明：
 */
public class LoadingActivity extends BaseActivity {

    private Class intoActivity;
    private String userLoginID;
    private boolean requestFlag = false;//判断网络是否请求结束
    private boolean timeOut = false;//判断3秒是否超时

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading_layout);
        userLoginID = SPHelper.getInstance(this).getStringData("userLoginID",null);
        if(TextUtils.isEmpty(userLoginID)){
            intoActivity = LoginActivity.class;
            requestFlag = true;
        } else {
            intoActivity = MainActivity.class;
            dorequestLogin();
        }
        handler.sendEmptyMessageDelayed(0, 500);
    }

    @Override
    protected void initVariable() {

    }

    @Override
    protected void setTitleViews() {

    }

    @Override
    protected void setViews() {

    }

    private void dorequestLogin(){
       params = new AjaxParams();
       httpRequest.addHeader("Cookie", "userLoginID=" + userLoginID);
       httpRequest.get(Urls.url_queryloginstatus,params,callBack,0);
    }

    @Override
    protected void handlerBean(int requestCode, BaseBean baseBean) {
        super.handlerBean(requestCode, baseBean);
        LogUtil.e(baseBean.toString()+"-----");
        requestFlag = true;
        if(baseBean.success){
            cookieHandler.sendEmptyMessageDelayed(0, 200);
            String data = String.valueOf(baseBean.data);
            LoginBean bean = gson.fromJson(data, LoginBean.class);
            MyApplication.loginBean = bean;
            MyApplication.USERID = bean.id;
            intoActivity = MainActivity.class;
        } else {
            intoActivity = LoginActivity.class;
        }

        if(timeOut){
            Intent intent = new Intent(LoadingActivity.this, intoActivity);
            startActivity(intent);
            AnimUtil.pushLeftInAndOut(LoadingActivity.this);
            LoadingActivity.this.finish();
        }

    }

    @Override
    public void handleNetErr(int requestCode, int errorCode) {
        super.handleNetErr(requestCode, errorCode);
        requestFlag = true;
        intoActivity = LoginActivity.class;
        if(timeOut){
            Intent intent = new Intent(LoadingActivity.this, intoActivity);
            startActivity(intent);
            AnimUtil.pushLeftInAndOut(LoadingActivity.this);
            LoadingActivity.this.finish();
        }
    }

    private Handler cookieHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            DefaultHttpClient httpClient;
            httpClient = (DefaultHttpClient) httpRequest.getHttpClient();
            CookieStore mCookieStore = httpClient.getCookieStore();
            List<Cookie> cookies = mCookieStore.getCookies();
            for (int i = 0; i < cookies.size(); i++) {
                LogUtil.e(cookies.get(i).getName() + "  " + cookies.get(i).getValue());
                if("userLoginID".equals(cookies.get(i).getName())){
                    SPHelper.getInstance(LoadingActivity.this).setStringData("userLoginID",cookies.get(i).getValue());
                }
            }
        };
    };


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            timeOut = true;
            if(requestFlag){
                Intent intent = new Intent(LoadingActivity.this, intoActivity);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(LoadingActivity.this);
                LoadingActivity.this.finish();
            }
        }
    };
}
